<template>
    <div class="main">
        <div class="header">
            <div class="logo">
                <span class="big" style="text-shadow: 4px 4px 5px #777,-4px -4px 5px #e8e8e8;font-size: 24px">{{siteName}}</span>
                <span class="min" style="text-shadow: 4px 4px 5px #777,-4px -4px 5px #e8e8e8;font-size: 32px">SP</span>
            </div>
            <span class="header-btn" @click="hiddenSidebar">
                <i class="el-icon-menu"></i>
            </span>
            <div class="time-box">
                <span class="time-time" v-text="datetime.time">12:00:00</span>
                <span class="time-date" v-text="datetime.date">2019-01-01</span>
            </div>
            <div class="right">
                <transition name="el-fade-in-linear">
                    <div v-show="infoShow" style="width: 50px;height: 50px;font-size: 30px;line-height: 50px;position: absolute;right: 216px;color:red;cursor: pointer" @click="openMessage">
                        <el-badge v-show="infoCount>0" :value="infoCount" style="top: 8px;">
                            <i class="el-icon-bell"></i>
                        </el-badge>
                    </div>
                </transition>
                <img :src="returnImgUrl"/>
                <el-dropdown>
                     <span class="header-btn">
                        {{currentUser.account}}（{{currentUser.roleId | RoleNameFilter}}）<i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="updatePassword"><i style="padding-right: 8px" class="el-icon-share"></i>修改密码</el-dropdown-item>
                        <el-dropdown-item @click.native="logout"><i style="padding-right: 8px" class="el-icon-star-off"></i>退出系统</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
        <div class="app">
            <div class="aside">
                <div class="menu">
                    <el-menu
                            router
                            class="menu"
                            text-color="black"
                            :unique-opened="uniqueOpened"
                            collapse-transition
                            :collapse="isCollapse"
                            :default-active="$route.path"
                            @open="handleOpen"
                            @close="handleClose">
                        <template v-for="(menu_v,menu_k) in menus">
                            <el-submenu v-if="menu_v.children.length>0" :key="menu_k" :index="menu_v.url">
                                <template slot="title">
                                    <i :class="menu_v.icon"></i>
                                    <span>{{menu_v.name}}</span>
                                </template>
                                <el-menu-item v-for="(menuChildren_v,menuChildren_k) in menu_v.children"
                                              :key="menuChildren_k"
                                              :index="menuChildren_v.url">
                                    <i :class="menuChildren_v.icon"></i>
                                    <span slot="title">{{ menuChildren_v.name }}</span>
                                </el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else :index="menu_v.url" :key="menu_k">
                                <i :class="menu_v.icon" ></i>
                                <span slot="title">{{menu_v.name}}</span>
                            </el-menu-item>
                        </template>
                    </el-menu>
                </div>
                <div class="sidebar-toggle" @click="sidebarToggle">
                    <div class="icon-left">
                        <i class="el-icon-back"></i>
                    </div>
                </div>
            </div>
            <div class="app-body">
                <div style="margin-top: 50px"></div>
                <div id="mainContainer" class="main-container">
                    <div class="top-nav" style="background-color: rgba(0, 0, 0, 0.54);position: fixed;top: 50px;width: 100%;z-index: 999">
                        <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;line-height: 16px;padding: 10px;background-color: rgba(0, 0, 0, 0.54)">
                            <el-breadcrumb-item>{{item1}}</el-breadcrumb-item>
                            <el-breadcrumb-item>{{item2}}</el-breadcrumb-item>
                        </el-breadcrumb>
                    </div>
                    <div class="main-body">
                       <router-view></router-view>
                    </div>
                </div>
            </div>
        </div>
        <el-dialog title="修改密码" :visible.sync="addDialogVisible"
                   width="30%">
            <el-form :model="userForm" ref="userForm" style="padding-right: 20px" :rules="rules" label-width="20%">
                <el-form-item label="用户名">
                    <el-input v-model="userForm.account" placeholder="用户名" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="loginPassword">
                    <el-input type="password" v-model="userForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="userForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="info" @click="updateUser(userForm)">确认</el-button>
                    <el-button @click="addDialogVisible=false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
    import Message from '../components/messages/index'
    import Filter from '../utils/filter'
    import Config from '../config'
    export default {
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.userForm.checkPass !== '') {
                        this.$refs.userForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.userForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                infoShow:true,
                menus:[],
                menuMap:{},
                addDialogVisible:false,
                isCollapse:false,
                siteName:'Smart Park',
                userInfo:{},
                uniqueOpened:true,
                rules:{
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ]
                },
                userForm:{
                    account: '',
                    password: '',
                    checkPass:''
                },
                menu:[],
                datetime:{
                    date:"",
                    time:""
                },
                currentUser : JSON.parse(localStorage.getItem("user")),
                // domain:"http://pno0766j2.bkt.clouddn.com/",
                domain: Config.domain,
                menusMap:{},
                infoCount:0

            }
        },
        methods: {
            NavBarWidth(){
                let navBar = document.getElementById('app');
                if(!navBar)return;
                let sidebarClose =  document.body.classList.contains('sidebar-close');
                if(sidebarClose) {navBar.style.width = 'calc(100%)';return;}
                if(this.isCollapse) navBar.style.width = 'calc(100%)';
                else navBar.style.width = 'calc(100%)';
            },
            hiddenSidebar(e) {
                e.preventDefault();
                document.body.classList.toggle('sidebar-close');
                this.NavBarWidth();
            },
            sidebarToggle(e) {
                e.preventDefault();
                if (this.isCollapse) {
                    document.body.classList.remove('sidebar-hidden');
                    this.siteName = "Smart Park";
                    this.isCollapse = false;
                } else {
                    document.body.classList.add('sidebar-hidden');
                    this.isCollapse = true;
                }
                this.NavBarWidth();
            },
            async logout(){
                this.$http.get("/user/loginOut").then(res=>{
                    if (res.status===200){
                        Message.success(res.data);
                        localStorage.clear();
                        this.$router.push("/login")
                    }
                })
            },

            handleOpen(){
            },
            handleClose(){},
            updatePassword(){
                this.userForm = JSON.parse(localStorage.getItem("user"));
                this.addDialogVisible = true;
            },
            async updateUser(data){
                let self = this;
                self.$http.post("/user/update",data).then(res=>{
                    if (res.status===200){
                        Message.success("修改成功！请重新登录");
                        localStorage.clear();
                        self.addDialogVisible = false;
                        self.$router.push("/login");
                    }
                });
                self.addDialogVisible = false;
            },
            getDatetime(){
                setInterval(()=>{
                    let date = new Date();
                    this.datetime.date = date.getFullYear()+"-"+((date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1))+"-"+(date.getDate()<10?"0"+date.getDate():date.getDate());
                    this.datetime.time = (date.getHours()<10?"0"+date.getHours():date.getHours())+":"+(date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes())+":"+(date.getSeconds()<10?"0"+date.getSeconds():date.getSeconds());
                },1000);
            },
            setTimer(){
                let self = this;
                if(self.timer == null) {
                    self.timer = setInterval( () => {
                        self.infoShow = !self.infoShow;
                    }, 1000)
                }
            },
            getUnReadInfo(){
                let user = JSON.parse(localStorage.getItem("user"));
                this.$http.get("/information/getUnReadInfoCount",{params:{"userId":user.id}}).then(res=>{
                    if (res.status===200){
                        this.infoCount = res.data;
                        if (this.infoCount>0){
                            this.setTimer();
                        }
                    }
                })
            },
            openMessage(){
                this.$router.push("/info");
            }
        },
        computed:{
            returnImgUrl:function () {
                if (this.currentUser.photo !== ""){
                    return this.domain+this.currentUser.photo;
                } else {
                    return ""
                }
            },
            item1(){
                const arr = this.$route.path.split("/");
                return this.menusMap[arr[1]].name;
            },
            item2(){
                const arr = this.$route.path.split("/");
                return this.menusMap[arr[1]][arr[2]];
            }
        },
        created(){
            this.getDatetime();
            this.menus =  JSON.parse(localStorage.getItem("menu"));
            this.menus.forEach(m => {
                const path1 = m.url.slice(1);
                this.menusMap[path1] ={name: m.name};
                if (m.children.length>0) {
                    m.children.forEach(c => {
                        this.menusMap[path1][c.url.split("/")[2]] = c.name;
                    })
                }

            });
            this.getUnReadInfo();
        },
        filters:{
            RoleNameFilter:function (data) {
                return Filter.RoleFilter(data);
            }
        }
    }
</script>
<style lang="less">
    .sidebar-hidden {
        .header {
            .logo {
                .big {
                    display: none;
                }
                .min {
                    display: block;
                }
                width: 64px;
            }

        }
        .aside{
            .sidebar-toggle{
                .icon-left{
                    transform: rotate(180deg);
                }
            }
        }
        .main {
            .app-body {
                height: 100%;
                margin-left: 64px;
            }
        }
    }
    .sidebar-close{
        .header {
            .logo {
                width: 0;
                overflow: hidden;
            }
        }
        .aside{
            margin-left: -200px;
        }
        .main {
            .app-body {
                margin-left: 0;
            }
        }
    }
    .sidebar-hidden.sidebar-close{
        .aside{
            margin-left: -64px;
        }
    }
    .main {
        display: flex;
        height: 100%;
        .el-menu:not(.el-menu--collapse) {
            width: 200px;
        }
        .el-menu{
            background-color: rgba(236, 245, 255, 0.5);
            /*font-family: "Microsoft YaHei";*/
            font-weight: bold;
            font-size: 28px;
            color: #E5FCFF;
            letter-spacing: 5px;
            text-shadow: 0 0 8px #FFF, 0 0 42px #FFF, 0 0 72px #FFF, 0 0 150px #FFF;
        }
        .app {
            width: 100%;
            background-color: #E2F1FF;
        }
        .aside {
            position: fixed;
            margin-top: 50px;
            z-index: 10;
            background-color: #7FA1BE;
            transition: all 0.3s ease-in-out;
            .menu{
                font-size: 20px;
                overflow-y: auto;
                overflow-x: hidden;
                height: calc(~'100vh - 100px');
                background-color: azure;
            }
            .sidebar-toggle{
                position: relative;
                width: 100%;
                height: 50px;
                background-color: #7FA1BE;
                color: #FFF;
                cursor: pointer;
                .icon-left{
                    position: absolute;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    right: 0;
                    width: 64px;
                    height: 100%;
                    font-size: 20px;
                    transition: all 0.3s ease-in-out;
                }
            }
        }
        .app-body {
            margin-left: 200px;
            -webkit-transition: margin-left 0.3s ease-in-out;
            transition: margin-left 0.3s ease-in-out;
        }
        .main-container {
            //margin-top: 50px;
            padding: 0;
            .main-body{
                height:  calc(~'100vh - 100px');
                padding-top: 35px;
            }
        }
    }

    .header {
        width: 100%;
        position: fixed;
        display: flex;
        height: 50px;
        background-color: #7FA1BE;
        z-index: 10;
        .logo {
            .min {
                display: none;
            }
            width: 200px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            color: #FFF;
            font-weight: 600;
            background-color: #7FA1BE;
            -webkit-transition: width 0.35s;
            transition: all 0.3s ease-in-out;
        }
        .right {
            position: absolute;
            right: 0;
            img{
                height: 48px;
                width: 48px;
                border-radius: 50%;
            }
        }
        .header-btn {
            .el-badge__content {
                top: 14px;
                right: 7px;
                text-align: center;
                font-size: 9px;
                padding: 0 3px;
                background-color: #00a65a;
                color: #E2F1FF;
                border: none;
                white-space: nowrap;
                vertical-align: baseline;
                border-radius: .25em;
            }
            overflow: hidden;
            height: 50px;
            display: inline-block;
            text-align: center;
            line-height: 50px;
            cursor: pointer;
            padding: 0 14px;
            color: #FFF;
            &:hover {
                background-color: #7FA1BE
            }
        }
        .time-box{
            display: flex;
            flex-direction: column;
            padding: 12px;
            .time-time{
                font-size: 20px;
                color: #8EF3FA;
                letter-spacing: 1.66px;
            }
            .time-date{
                opacity: 0.48;
                font-size: 10px;
                -webkit-transform: scale(0.83333333) translate(-8.33333333%, 0);
                transform: scale(0.83333333) translate(-8.33333333%, 0);
                color: #8EF3FA;
                letter-spacing: 0.83px;
                text-indent: 34px;
            }
        }
    }

    .menu {
        border-right: none;
    }

    .el-menu--vertical {
        min-width: 190px;
    }
    .setting-category{
        padding:10px 0;
        border-bottom: 1px solid #eee;
    }
    ::-webkit-scrollbar
    {
        width: 5px;
        height: 5px;
        background-color: #F5F5F5;
    }
    /*定义滚动条轨道 内阴影+圆角*/
    ::-webkit-scrollbar-track
    {
        -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
        border-radius: 10px;
        background-color: #F5F5F5;
    }
    /*定义滑块 内阴影+圆角*/
    ::-webkit-scrollbar-thumb
    {
        border-radius: 10px;
        -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
        background-color: #bdbdbd;
    }
    /*滑块效果*/
    ::-webkit-scrollbar-thumb:hover
    {
        border-radius: 5px;
        -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
        background: rgba(0,0,0,0.4);
    }
    /*IE滚动条颜色*/
    html {
        scrollbar-face-color:#bfbfbf;/*滚动条颜色*/
        scrollbar-highlight-color:#000;
        scrollbar-3dlight-color:#000;
        scrollbar-darkshadow-color:#000;
        scrollbar-Shadow-color:#adadad;/*滑块边色*/
        scrollbar-arrow-color:rgba(0,0,0,0.4);/*箭头颜色*/
        scrollbar-track-color:#eeeeee;/*背景颜色*/
    }

</style>
